/*************************************************************************
	> File Name: set_remove-attr.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: Mon 07 Apr 2014 06:27:30 PM CST
 ************************************************************************/

#include<stdio.h>
#include<sys/xattr.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#define SIZE 1024
int main(int argc, char *argv[])
{
    int opt, opt_ind,len=0;
   // char *option_n, *option_x;
    int trueOfalse=0;

    while((opt=getopt(argc,argv,"xnd:v:"))!=-1)
    {
       // opt_ind=optind?optind:1;             
                                                    /*
                                                   if(optind)
                                                   opt_ind=optind;
                                                   else
                                                   optind=1;   系统默认将optind 初始化为一, 但对argv参数扫描是会scan argv[0] 及文件名,所以用这句将optind置 1
                                                   */

        switch(opt)
        {
/**
 * ******************* now setxattr is correct**************************
 * */
            case 'n':
                printf("now set the attr\n");
                int set;
                if(argc<4)
                {
                    printf("too little para\n");
                    exit(0);
                }
                else if(argc==4)
                {
                    
                    if((set=setxattr(argv[2],argv[3],NULL,0,XATTR_CREATE))!=-1) //不带 -v 参数时，即不带value NULL长度为0
                    {
                        printf("set attr succeeded\n");
                        trueOfalse=1;
                    }
                    else
                    {
                        perror("setxattr");
                        exit(0);
                    }
                }

                else if(argc>5)
                {
                    while(argv[5][len])
                    {
                        len++;
                    }
                    if((set=setxattr(argv[3],argv[4],argv[5],len,XATTR_CREATE))!=-1)//带 -v参数，即 带有 value argv[5] 长度为len
                    {
                        printf("set attr succeeded\n");
                        trueOfalse=1;
                    }
                    else
                    {
                        perror("setxattr");
                        exit(0);
                    }
                }
                break;
/**
 * *************** the remove part is correct ***********************
 * */
            case 'x':
                printf("now remove the attr\n");
                int re;
                if(argc<3)
                {
                    printf("too little para :(\n");
                    exit(0);
                }
                else if((re=removexattr(argv[2],argv[3]))!=-1)
                {
                    printf("remove succeeded\n");
                        trueOfalse=1;
                }
                else
                {
                    perror("removexattr");
                    exit(0);
                }
                break;
/******************* need to be completed **************************/
            case 'd':
                printf("now list attr\n");
                char value[1024];
                if(argc<4)
                {
                    printf("too littile para :(\n\nrun by ./%s -d pathname user.xxx\n",argv[0]);
                    exit(0);
                }

                if(getxattr(argv[2],argv[3],value,SIZE)!=-1)
                {
                    printf("the attr is %s \n",value);
                        trueOfalse=1;
                }
                break;

            default:
                if(trueOfalse)
                    exit(0);
                else
                printf("run by ./%s -x pathname name\n or\n ./%s -n pathname name value size\n",argv[2],argv[2]);
        }
    }
    exit(0);
}
